package com.mrwang.controller;

import com.mrwang.pojo.SpecGroup;
import com.mrwang.pojo.SpecParam;
import com.mrwang.service.SpecService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {
    @Resource
    SpecService specService;
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> groupByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> groupList= specService.groupByCid(cid);
        if (CollectionUtils.isEmpty(groupList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groupList);
    }





    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> paramList(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        List<SpecParam> specParamList= specService.paramList(gid,cid,generic,searching);
        if (CollectionUtils.isEmpty(specParamList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParamList);
    }







    @PostMapping("param")
    public ResponseEntity saveParam(@RequestBody SpecParam specParam){
            specService.saveParam(specParam);
            return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @DeleteMapping("param/{id}")
    public ResponseEntity deleteSpecParam(@PathVariable(value = "id") Long id){
        specService.deleteSpecParam(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping("param")
    public ResponseEntity updateParam(@RequestBody SpecParam specParam){
            specService.updateParam(specParam);
            return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
